package com.sangmo.fts.process.inventory.service;

import com.sangmo.boot.framework.plugin.dataview.crud.service.BaseViewService;
import com.sangmo.boot.framework.plugin.dataview.data.IDataViewRepo;
import com.sangmo.fts.common.authority.service.AuthorityEntityService;
import com.sangmo.fts.process.inventory.interfaces.IfInboundCreation;
import com.sangmo.fts.process.inventory.model.IfInbound;
import com.sangmo.fts.process.inventory.model.IfLadingBill;
import com.sangmo.fts.process.inventory.model.IfOutbound;
import com.sangmo.fts.process.inventory.repo.IfInboundRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class IfInboundEntityService extends AuthorityEntityService<IfInbound> {

    @Autowired
    private IfInboundRepo ifInboundRepo;

    @Autowired
    private IfOutBoundService ifOutBoundService;

    @Autowired
    private IfLadingBillService ifLadingBillService;

    @Override
    protected IDataViewRepo<IfInbound> getRepo() {
        return ifInboundRepo;
    }

    @Override
    protected void onCreate(IfInbound entity, Object bo) {
        super.onCreate(entity, bo);
        IfInboundCreation creation = (IfInboundCreation) bo;
        IfOutbound outbound = ifOutBoundService.get(creation.getOutboundId());
        entity.setOutboundId(outbound.getId());
        entity.setBillId(outbound.getBillId());
        IfLadingBill ladingBill = ifLadingBillService.get(outbound.getBillId());
        entity.setLoanId(ladingBill.getLoanId());
        entity.setPledgee(outbound.getPledgee());
        entity.setPledgor(outbound.getPledgor());
        entity.setRegulator(outbound.getRegulator());
    }
}
